package com.huike.web.controller.report;


import com.huike.clues.domain.dto.AssignRecordDto;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.service.ITbAssignRecordService;
import com.huike.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

@RestController
@RequestMapping("/index")
public class IndexController {


    @Autowired
    ITbAssignRecordService iTbAssignRecordService;

    @Autowired
    TbAssignRecordMapper tbAssignRecordMapper;


    //线索转化龙虎榜
    @GetMapping("/salesStatistic")
    public AjaxResult index(String beginCreateTime, String endCreateTime) {


        //用户线索转化的集合
        List<AssignRecordDto> list = iTbAssignRecordService.select();


        //每个用户的线索总数集合
        List<Integer> cluesByUser = tbAssignRecordMapper.selectAllCluesByUser();

        ArrayList<HashMap> list1 = new ArrayList<>();

        list.forEach(new Consumer<AssignRecordDto>() {
            @Override
            public void accept(AssignRecordDto assignRecordDto) {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("create_by", assignRecordDto.getUserName());
                //每个用户转化总数
                hashMap.put("num", assignRecordDto.getNums().toString());

                hashMap.put("dep_id", assignRecordDto.getDeptId().toString());
                hashMap.put("deptName", assignRecordDto.getDeptName());

                //每个用户线索总数 == 得到转化率
                for (Integer integer : cluesByUser) {
                    System.out.println(assignRecordDto.getNums()+"---------");
                    System.out.println(integer);
                   // hashMap.put("radio",assignRecordDto.getNums()/integer+"");
                    hashMap.put("radio",new BigDecimal((float)assignRecordDto.getNums()/integer).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()*100+"");

                }
                list1.add(hashMap);
            }
        });



      //转化率


        return AjaxResult.success(list1);
    }

}
